package com.yishan.controller;

import com.github.pagehelper.PageInfo;
import com.yishan.domain.Dept;
import com.yishan.domain.Module;
import com.yishan.domain.Role;
import com.yishan.domain.User;
import com.yishan.service.DeptService;
import com.yishan.service.RoleService;
import com.yishan.service.UserService;
import com.yishan.service.UserService;
import com.yishan.utils.BeanUtil;
import java.io.IOException;
import java.util.List;
import javax.jws.WebParam.Mode;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author yishan
 * @Date 2021/4/12 0012 20:53
 * @Version 1.0
 */

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private DeptService deptService;

	@Autowired
	private RoleService roleService;


	/**
	 * 查询所有
	 *
	 * @param model
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/list")
	private String list(Model model, HttpServletRequest request)
			throws ServletException, IOException {

		int page = 1;
		int size = 5;

		if (StringUtils.isNotBlank(request.getParameter("page"))) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (StringUtils.isNotBlank(request.getParameter("size"))) {
			size = Integer.parseInt(request.getParameter("size"));
		}

		PageInfo info = userService.findAll(page, size);

		model.addAttribute("page", info);

		return "/WEB-INF/pages/user/list.jsp";

	}

	/**
	 * 跳转到添加页面
	 *
	 * @return
	 */
	@RequestMapping("/toAdd")
	private String toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//修改需要查询出部门的信息
		List<Dept> deptList = deptService.findAll();
		request.setAttribute("deptList", deptList);

		request.getRequestDispatcher("/WEB-INF/pages/user/add.jsp")
				.forward(request, response);

		return "/WEB-INF/pages/user/add.jsp";

	}

	/**
	 * 添加操作
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/save")
	private String save(HttpServletRequest request) {

		User user = BeanUtil.fillBean(request, User.class);
		userService.save(user);

		return "redirect:" + request.getContextPath() + "/user/list";

	}

	/**
	 * 跳转到修改页面
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toEdit")
	private String toEdit(String id, Model model) {

		User user = userService.findById(id);

		model.addAttribute("user", user);

		//修改需要查询出部门的信息
		List<Dept> deptList = deptService.findAll();
		model.addAttribute("deptList", deptList);

		return "/WEB-INF/pages/user/update.jsp";

	}

	/**
	 * 修改操作
	 *
	 * @param user
	 * @return
	 */
	@RequestMapping("/edit")
	private String edit(User user) {

		userService.update(user);

		return "redirect:/user/list";

	}

	/**
	 * 删除操作
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	private String delete(String ids) {
		System.out.println(ids);

		userService.deleteMany(ids);

		return "redirect:/user/list";

	}

	@RequestMapping("/userRoleList")
	public String userRoleList(@RequestParam("id") String userId, Model model) {

		String[] ids = userId.split(",");
		String id = ids[0];

		//获取所有的角色列表
		List<Role> roleList = roleService.findAllRoleByUserId(id);
		model.addAttribute("roleList", roleList);

		//获取user
		User user = userService.findById(id);
		model.addAttribute("user", user);

		return "/WEB-INF/pages/user/role.jsp";
	}

	@RequestMapping("/updateRole")
	public String updateRole(HttpServletRequest request, HttpServletResponse response) {

		String userId = request.getParameter("userId");
		String[] roleIds = request.getParameterValues("roleIds");

		userService.updateRole(userId, roleIds);

		return "redirect:/user/list";
	}

	/**
	 * 登录
	 *
	 * @param email
	 * @param zd_pwd
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String email, String zd_pwd, HttpServletRequest request) {

		User user = userService.login(email, zd_pwd);

		if (null == user) {
			return "/login.jsp";
		}

		request.getSession().setAttribute("loginUser", user);

		//如果登录成功，加载此用户对用的角色和对应的所有模块
		List<Module> moduleList = userService.findModuleById(user.getId());
		request.setAttribute("moduleList",moduleList);

		//跳转到主页面
		return "/WEB-INF/pages/home/main.jsp";

	}

	/**
	 * 跳转到后台页面
	 *
	 * @return
	 */
	@RequestMapping("/home")
	public String home() {
		//跳转到后台主页面
		return "/WEB-INF/pages/home/home.jsp";
	}


	/**
	 * 注销
	 * @param email
	 * @param zd_pwd
	 * @param request
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {

		request.getSession().removeAttribute("loginUser");
		//跳转到主页面
		return "/login.jsp";

	}
}
